/**
 * SAML Attribute model.
 * UserDocument {
 *  inResponseTo: ID of samlp:AuthnRequest
 *  loginId: gynaru.park
 *  email: gynaru.park@samsung.com
 *  nameKr: 박경엽
 *  nameEn: Kyoungyoup Park
 *  groupNameKr: 시스템개발그룹(BDC)
 *  groupNameEn: System R&D Group
 *  departmentId: 21020318C10A8906
 * }
 */
export interface UserDocument extends Record<string, any | undefined> {
  inResponseTo?: string;
  loginId?: string;
  email?: string;
  nameKr?: string;
  nameEn?: string;
  groupNameKr?: string;
  groupNameEn?: string;
  departmentId?: string;
}

/**
 * Department chain in which user belogins to:
 *
 * e.g.)
 * "epdeptlevel": "2",
 * "ependepartment": "Big Data Center",
 * "ou": "Big Data센터",
 * "departmentnumber": "20018874C10A8900",
 * "ephighdeptnumber": "C10S1001"
 */
export interface Department {
  epdeptlevel: string;
  ependepartment: string;
  ou: string;
  departmentnumber: string;
  ephighdeptnumber: string;
}

export interface DepartmentChain extends Array<Department> {}

/**
 * GET /v1/users?loginId={loginId} model.
 *
 * UserSessionInfo {
 * "uid": "f130cab0-e559-4ff2-b491-df68ab4a772e",
 * "info": {
 *    loginid: guest
 *    email: guest@samsung.com
 *    username: Samsung Kim
 *    ugroup: System R&D Group
 *  }
 * }
 */
export interface UserSessionInfo extends Record<string, any | undefined> {
  uid?: string;
  info: UserInfo;
  status: UserStatus;
  adfs?: UserDocument;
}

/**
 * POST /v1/users API model.
 *
 * UserInfo {
 *  email: 'guest@samsung.com',
 *  loginid: 'guest',
 *  ugroup: 'System R&D Group',
 *  username: 'Samsung Kim',
 * }
 *
 */
export interface UserInfo {
  email?: string;
  loginid?: string;
  ugroup?: string;
  username?: string;
  role?: string;
  depId?: string; // ONLY requied for registration process.
}

export interface UserStatus {
  loginStatus: string;
  lastLoginTime?: string;
  createdTime?: string;
}

export const getNewComerFromDocument = (document: UserDocument): UserSessionInfo => {
  return {
    info: {
      email: document.email,
      loginid: document.loginId,
      ugroup: document.groupNameEn,
      username: document.nameEn,
      role: UserRole.Customer,
    },
    status: {
      loginStatus: UserLoginStatus.NewComer,
    },
    adfs: document,
  };
};

/**
 * User Roles and Sign-Up status
 * for simple RBAC (Role Based Access Control)
 */
export enum UserRole {
  Admin = 'Admin',
  Customer = 'Customer',
}

/**
 * User Login Status
 *
 * NewComer: Newly visited user for the first time.
 * Pending: Waiting for approval of enrollment request.
 * Approved: Request is approved and enrollment is done.
 * Rejected: Request is rejected and enrollment is done.
 * Inactive: Inactive account because user does not login to service for a long time.
 * Revoked: Approval is revoked because user does not belongs to allowed group anymore.
 */
export enum UserLoginStatus {
  NewComer = 'NewComer',
  Pending = 'Pending',
  Approved = 'Approved',
  Rejected = 'Rejected',
  Inactive = 'Inactive',
  Revoked = 'Revoked',
}
